<template>
  <el-dialog :title="form.id ? $t('btn.edit') : $t('operation.add')" v-model="visible" width="700"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="120px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('iot.processCode')" prop="checkProcessCode">
            <el-select :placeholder="$t('iot.processCode')"
                       filterable
                       remote
                       @change="changeCheckType"
                       :remote-method="getProcessList"
                       v-model="form.checkProcessCode">
              <el-option v-for="item in processList"
                         :key="item.text"
                         :label="item.text"
                         :value="item">
                <span style="float: left">{{ item.text }}</span>
                <span style=" float: right;  color: var(--el-text-color-secondary);  font-size: 13px; "> {{
                    item.value
                  }} </span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.productionCheckItemCode')" prop="productionCheckCode">
            <el-input v-model="form.productionCheckCode" :placeholder="$t('qms.productionCheckItemCode')"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.productionCheckItemName')" prop="productionCheckName">
            <el-input v-model="form.productionCheckName" :placeholder="$t('qms.productionCheckItemName')"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.enableOrNot')" prop="isEnable">
            <el-radio-group v-model="form.isEnable">
              <el-radio :key="index" :label="item.value" border v-for="(item, index) in YN">{{
                  item.label === 'Y' ? $t('qms.yes') : $t('qms.no')
                }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.checkLocationCode')" prop="checkLocationCode">
            <el-input v-model="form.checkLocationCode" :placeholder="$t('qms.checkLocationCode')"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('qms.checkLocationName')" prop="checkLocationName">
            <el-input v-model="form.checkLocationName" :placeholder="$t('qms.checkLocationName')"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('basDicSet.remark')" prop="remark">
            <el-input v-model="form.remark" :placeholder="$t('basDicSet.remark')"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">{{$t('operation.Cancel')}}</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">{{$t('operation.confirm')}}</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="QmsProductionCheckDialog">
import {useMessage} from "/@/hooks/message";
import {addObj, getObj, putObj} from '/@/api/platform/qms/qmsProductionCheck'
import {fetchCheckList} from "/@/api/platform/qms/qmsRelationH";
import {useDict} from "/@/hooks/dict";
import {useI18n} from "vue-i18n";

const {t} = useI18n();

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const processList = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
  id: '',
  checkProcessCode: '',
  checkProcessName: '',
  productionCheckCode: '',
  productionCheckName: '',
  remark: '',
  isEnable: '',
  creationTime: '',
  createUserNo: '',
  createUserName: '',
  lastModifyTime: '',
  lastModifyUserName: '',
  lastModifyUserNo: '',
  attribute1: '',
  attribute2: '',
  attribute3: '',
});

// 定义校验规则
const dataRules = ref({
  checkProcessCode: [
    {required: true, trigger: 'blur'}
  ], productionCheckCode: [
    {required: true, trigger: 'blur'}
  ],productionCheckName: [
    {required: true, trigger: 'blur'}
  ],isEnable: [
    {required: true, trigger: 'blur'}
  ],
})

const {YN} = useDict('YN');

//获取工序
const getProcessList = () => {
  fetchCheckList({
    "code": "ProductionProcesses"
  }).then((res: any) => {
    if (res.code == 0) {
      processList.value = res.data
    }
  })
}

const changeCheckType = (data) =>{
  form.checkProcessCode = data.value
  form.checkProcessName = data.text
}

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取qmsProductionCheck信息
  if (id) {
    form.id = id
    getqmsProductionCheckData(id)
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? t('qms.modifiedSuccessfully') : t('qms.addSuccessfully'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getqmsProductionCheckData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>